package com.easymin.weixin.controller.v4;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.easymin.weixin.common.api.DaijiaApi;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.WxApplicationManager;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController("daijiaResV4")
@RequestMapping("api/daijia/v4")
public class DaijiaRes {
  @Autowired
  private DaijiaApi daijiaApi;
  @Autowired
  private WxApplicationManager wxApplicationManager;

  /**
   * 代驾订单结算页面的数据V4.
   */
  @RequestMapping(value = {"completeTaskData"}, method = RequestMethod.GET)
  public ResultTransport completeTask(
      @RequestParam("orderId") Long orderId,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport resultTransport =
        daijiaApi.completeTaskDaijiaV4(wxApplication.getAddress(), orderId);
      return resultTransport;
  }

  /**
   * 确认代驾订单费用的接口V4.
   * @param orderId 订单id
   * @param realCash 实付金额
   * @param guoluMoney 过路费
   * @param yuanchengMoney 远程费
   * @param otherMoney 其他费
   * @param mileage 里程
   * @param waitedTime 等待时间
   * @param travelTime 行驶时间
   * @param toPlace 实际目的地
   * @param toPlaceLng 目的地详情经度
   * @param toPlaceLat 目的地详情纬度
   */
  @RequestMapping(value = {"confirmTask"}, method = RequestMethod.POST)
  public ResultTransport confirmTask(
      @RequestParam("orderId") Long orderId,
      @RequestParam("realCash") Double realCash,
      @RequestParam("guoluMoney") Double guoluMoney,
      @RequestParam("yuanchengMoney") Double yuanchengMoney,
      @RequestParam("otherMoney") Double otherMoney,
      @RequestParam("mileage") Double mileage,
      @RequestParam("waitedTime") Integer waitedTime,
      @RequestParam("travelTime") Integer travelTime,
      @RequestParam(required = false, value = "startPrice") Double startPrice, 
      @RequestParam(required = false, value = "mileagePrice") Double mileagePrice, 
      @RequestParam(required = false, value = "runTimePrice") Double runTimePrice,
      @RequestParam(required = false, value = "toPlace") String toPlace,
      @RequestParam(required = false, value = "toPlaceLng") Double toPlaceLng, 
      @RequestParam(required = false, value = "toPlaceLat") Double toPlaceLat,
      @RequestParam(required = false, value = "memo") String memo,
      @RequestParam(required = false, value = "waitPrice") Double waitPrice,
      @RequestParam(required = false, value = "jsonLocs") String jsonLocs,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport resultTransport =
        daijiaApi.confirmTaskDaijiaV4(wxApplication.getAddress(), orderId, realCash, guoluMoney,
            yuanchengMoney, otherMoney, mileage, waitedTime, travelTime, startPrice, mileagePrice,
            runTimePrice, waitPrice, toPlace, toPlaceLng, toPlaceLat, memo, jsonLocs);

    return resultTransport;
  }

  /**
   * 实时获取订单是否被结算V4.
   */
  @RequestMapping(value = {"checkTask"}, method = RequestMethod.GET)
  public ResultTransport checkTask(
      @RequestParam("orderId") Long orderId,
      @RequestParam("appKey") String appKey) {

    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport resultTransport =
        daijiaApi.checkTaskDaijiaV4(wxApplication.getAddress(), orderId);

    return resultTransport;
  }

  /**
   * 最后结算接口V4.
   * @param payType 支付类型: helppay(代付) sign(签单) balance(余额)
   */
  @RequestMapping(value = {"finishTask"}, method = RequestMethod.POST)
  public ResultTransport finishTask(
      @RequestParam("orderId") Long orderId,
      @RequestParam("payType") String payType,
      @RequestParam(required = false, value = "who") String who,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport resultTransport =
        daijiaApi.finishTaskDaijiaV4(wxApplication.getAddress(), orderId, payType, who);

    return resultTransport;
  }

}
